//
//  HomeShowNoticeView.m
//  Base_App
//
//  Created by Yutian Duan on 2019/5/28.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "HomeShowNoticeView.h"

@implementation HomeShowNoticeView

+ (instancetype)noticeView {
  
  HomeShowNoticeView *alertView = [[HomeShowNoticeView alloc] init];
  return alertView;

}

- (instancetype)init {
  self = [super init];
  if (self) {
    
    self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    [self setDefalutView];

  }
  return self;
  
}

- (void)setDefalutView {
  
  ///!
  CGFloat imageWith = ScreenWidth - 40;
  
  
  UIView *contenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageWith , imageWith * 0.9)];
  contenView.backgroundColor = [UIColor whiteColor];
  contenView.layer.cornerRadius = 8.0f;
  contenView.center = CGPointMake(ScreenWidth/2, (ScreenHeight - TabBarHeight)/2);
  [self addSubview:contenView];
  
  CGFloat noticeH = 45;
  if (IS_IPHONE5) {
    noticeH = 30;
  }

  ///!
  UIImageView *noticeImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, noticeH, noticeH)];
  noticeImage.image = [UIImage imageNamed:@"Home_notice"];
  noticeImage.yt_centerX = contenView.yt_width/2;
  [contenView addSubview:noticeImage];
  
  ///!
  UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, noticeImage.yt_bottom + 9, contenView.yt_width, 28)];
  titleLabel.text = YTLocalizedString(@"即时接收最新资讯");
  titleLabel.textAlignment = NSTextAlignmentCenter;
  titleLabel.textColor = [UIColor blackColor];
  titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0f];
  [contenView addSubview:titleLabel];
  
  CGFloat offsetTop = 20;
  CGFloat buttonH = 24;
  CGFloat buttonTap = 30;
  CGFloat buttonOffsetX = 21;
  if (IS_IPHONE5) {
    offsetTop = 15;
    buttonH = 21;
    buttonTap = 20;
    buttonOffsetX = 14;
  }
  
  ///!
  UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, titleLabel.yt_bottom + offsetTop, contenView.yt_width - 60, 0)];
  contentLabel.numberOfLines = 0;
  contentLabel.text = YTLocalizedString(@"恒堡会目标");
  contentLabel.font = [UIFont systemFontOfSize:14.0f];
  [contentLabel sizeToFit];
  [contenView addSubview:contentLabel];

  ///! 拒绝
  UIButton *refusedButton = [[UIButton alloc] initWithFrame:CGRectMake(100, contenView.yt_height - buttonTap - 20, buttonH * 5, buttonH)];
//  [refusedButton setImage:[UIImage imageNamed:@"Home_refused_notice"] forState:UIControlStateNormal];
 
  refusedButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0f];
  refusedButton.backgroundColor = DEF_GRAY;
  [refusedButton setTitle:YTLocalizedString(@"暂不允许") forState:UIControlStateNormal];
  [refusedButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  refusedButton.layer.masksToBounds = YES;
  refusedButton.layer.cornerRadius = 8;
  
  [refusedButton addTarget:self action:@selector(refusedClick) forControlEvents:UIControlEventTouchUpInside];
  refusedButton.yt_right = contenView.yt_width/2 - buttonOffsetX;
  [contenView addSubview:refusedButton];
  
  //! 通过
  UIButton *passButton = [[UIButton alloc] initWithFrame:CGRectMake(refusedButton.yt_right + buttonOffsetX, refusedButton.yt_top, refusedButton.yt_width, refusedButton.yt_height)];
//  [passButton setBackgroundImage:[UIImage imageNamed:@"Home_pass_notice"] forState:UIControlStateNormal];

  passButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0f];
  passButton.backgroundColor = DEF_RED;
  [passButton setTitle:YTLocalizedString(@"设定") forState:UIControlStateNormal];
  [passButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  passButton.layer.masksToBounds = YES;
  passButton.layer.cornerRadius = 8;

  [passButton addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];

  [contenView addSubview:passButton];

  
}


- (void)showWithClose:(CloseClickBlock)clickBlock {
  self.clickBlock = clickBlock;
  UIWindow *window = [UIApplication sharedApplication].keyWindow;
  [window addSubview:self];
}

- (void)refusedClick {
  if (self.clickBlock) {
    self.clickBlock(NO);
  }
  [self closeView];

}

- (void)passClick {
  
  if (self.clickBlock) {
    self.clickBlock(YES);
  }
  [self closeView];
}

- (void)closeView {
  [self removeFromSuperview];
}

@end
